﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace CopyTool
{
    public partial class CopyTool : Form
    {
        public CopyTool()
        {
            InitializeComponent();
            this.txtLocalPath.Text = "d:\\tmdev\\trunk\\actual";
            System.IO.StreamReader file = null;
            try
            {
                file =
                    new System.IO.StreamReader("config.property");
            } catch(FileNotFoundException enfe) {
                return;
            }

            String line;
            while ((line = file.ReadLine()) != null)
            {
                String[] values = line.Split('=');
                txtLocalPath.Text = values[1];
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (txtAffectObjects.Lines.Length == 0)
            {
                MessageBox.Show("Please enter the affect objects.");
                return;
            }

            getServerPath();
            getLocalPath();

            //get affect objects
            String orig;
            String dest;
            String lineItr;
            foreach (String line in txtAffectObjects.Lines)
            {
                if (line == null || line.Trim().Length == 0 || line.Contains(serverPath))
                {
                    continue;
                }

                lineItr = line.Replace("/", "\\");
                String[] elements = lineItr.Split(new char[] { ' ' });
                String relativeDestPath = elements[1];

                if (relativeDestPath.Substring(0).Equals("\\"))
                {
                    relativeDestPath = relativeDestPath.Substring(1, relativeDestPath.Length - 1);
                }

                orig = serverPath + "\\" + relativeDestPath;
                dest = localPath + "\\" + relativeDestPath;
                try
                {
                    File.Copy(orig, dest, true);
                }
                catch (Exception)
                {
                    MessageBox.Show("Encounter erro when coping from:" + orig + "     to:" + dest + ".  Exception:" + e);
                    return;
                }
            }

            MessageBox.Show("Copy is done!");

        }

        private void getLocalPath()
        {
            localPath = getFilteredPath(txtLocalPath.Lines[0]);
        }

        private void getServerPath()
        {
            serverPath = getFilteredPath(txtAffectObjects.Lines[0]);
        }

        private String getFilteredPath(String path)
        {
            path = path.Replace("=", String.Empty);
            if (path.Substring(path.Length - 1).Equals("\\"))
            {
                path = path.Substring(0, path.Length - 1);
            }

            return path;
        }

        private String serverPath;
        private String localPath;

        private void btnGen_Click(object sender, EventArgs e)
        {
            if (txtAffectObjects.Lines.Length == 0)
            {
                MessageBox.Show("Please enter the affect objects.");
                return;
            }

            getServerPath();

            String svnCommand = "svn commit "; 

            txtSVNScript.Text = String.Empty;

            //get affect objects
            String lineItr;
            foreach (String line in txtAffectObjects.Lines)
            {
                if (line == null || line.Trim().Length == 0 || line.Contains(serverPath))
                {
                    continue;
                }

                lineItr = line.Replace("/", "\\");
                String[] elements = lineItr.Split(new char[] { ' ' });
                String relativeDestPath = elements[1];

                if (relativeDestPath.Substring(0).Equals("\\"))
                {
                    relativeDestPath = relativeDestPath.Substring(1, relativeDestPath.Length - 1);
                }

                svnCommand = svnCommand + " " + relativeDestPath;
                
            }

            svnCommand = svnCommand + " -m \"" + txtSVNCommitMsg.Text + "\"";

            txtSVNScript.Text = svnCommand;

            MessageBox.Show("Generate scripts are done!");
        }

        private void txtLocalPath_DoubleClick(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", txtLocalPath.Text);
        }

       
    }
}
